package com.example.qcrmdemo.Controller;

import com.example.qcrmdemo.entity.Customer;

import com.example.qcrmdemo.entity.Serve;
import com.example.qcrmdemo.service.SalesPlanService;
import com.example.qcrmdemo.service.ServeService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ServeController {

    private static final Logger logger = LoggerFactory.getLogger(ServeController.class);

    @Autowired
    private ServeService serveService;

    @GetMapping("/FuWuGuanLi/serveCreate")
    public ModelAndView ServeCreateView(){
        ModelAndView mav=new ModelAndView("FuWuGuanLi/serveCreate");
        //调用服务器方法查询分页客户数据
        PageInfo<Serve> page =serveService.selectAll();
        mav.addObject("page",page);
        return mav;
    }

    @GetMapping("/FuWuGuanLi/serveCreateNew")
    public ModelAndView ServeCreateNewView(){
        ModelAndView mav=new ModelAndView("FuWuGuanLi/serveCreateNew");
        return mav;
    }

    @PostMapping("/FuWuGuanLi/serveCreateNew")
    //Validated注解所标注的参数，表面该参数中的属性取值必须要按照属性上定义的验证规则进行验证，验证是由验证框架完成的
    //验证的结果会以BindingResult实例的形式传入到该方法
    public ModelAndView ServeCreateNew(@Validated Serve serve, BindingResult bindingResult){
        //redirect表示重定向响应
        //给浏览器仅仅只发送一个请求地址，而没有任何视图内容或者数据
        //浏览器会使用收到的地址，重新发送一个get请求，由于是新请求，因此没有缓存上一次请求的数据，从而达到防止重复提交表单的请求
        ModelAndView mav=new ModelAndView("redirect:/FuWuGuanLi/serveCreate");
        Map<String,String > errorMessage =new HashMap<>();
        if(bindingResult.hasErrors()){
            //如果有验证错误信息，那么要进行处理
            //获得出现错误域，以及相应的错误消息文本
            final List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            //错误放到映射中
            for (FieldError error:fieldErrors){
                //从每一个域错误对象中取出域名称，错误消息，加到映射中
                errorMessage.put(error.getField(),error.getDefaultMessage());
            }
            //将错误保存到视图
            mav.addObject("errorMessage",errorMessage);
            mav.addObject("serve",serve);
            //由于没有保存数据，因此不必进行重定向，内部转发请求到视图。同时，还要以保留请求数据，以及响应的信息
            mav.setViewName("FuWuGuanLi/serveCreateNew");
        }else{
            //如果没有有验证错误信息，验证通过，保存客户信息
            //调用服务方法保存客户数据
            Date time = new java.util.Date(new java.util.Date().getTime());
            serve.setCreateDate(time);
            serve.setUpdateDate(time);
            boolean result= serveService.append(serve);
            if(result){
                logger.info("新增服务成功 - " + serve);
            }else {
                logger.info("新增服务失败 - " + serve);
            }
        }
        return mav;
    }

    @GetMapping("/FuWuGuanLi/serveChuLi")
    public ModelAndView ServeChuLiView(){
        ModelAndView mav=new ModelAndView("FuWuGuanLi/serveChuLi");
        //调用服务器方法查询分页客户数据
        PageInfo<Serve> page =serveService.selectWeiChuLi();
        mav.addObject("page",page);
        return mav;
    }

    @GetMapping("/FuWuGuanLi/serveChuLiNew")
    public ModelAndView ServeChuLiNewView(
            @RequestParam(name="id" ,required = true)int id
    ){
        ModelAndView mav=new ModelAndView("FuWuGuanLi/serveChuLiNew");
        Serve serve=serveService.findById(id);
        mav.addObject("serve",serve);
        return mav;
    }

    @PostMapping("/FuWuGuanLi/serveChuLiNew")
    //Validated注解所标注的参数，表面该参数中的属性取值必须要按照属性上定义的验证规则进行验证，验证是由验证框架完成的
    //验证的结果会以BindingResult实例的形式传入到该方法
    public ModelAndView ServeChuLiNewNew(
            Serve serve
    ){
        String redirectUrl="redirect:/FuWuGuanLi/serveChuLi";
        ModelAndView mav=new ModelAndView(redirectUrl);
        Date time = new java.util.Date(new java.util.Date().getTime());
        serve.setUpdateDate(time);
        serve.setServiceProceTime(time);
        boolean result=serveService.modify(serve);
        if(result){
            logger.info("处理服务成功 - " + serve);
        }else {
            logger.info("处理服务失败 - " + serve);
        }
        return mav;
    }

    @GetMapping("/FuWuGuanLi/serveFanKui")
    public ModelAndView ServeFanKuiView(){
        ModelAndView mav=new ModelAndView("FuWuGuanLi/serveFanKui");
        PageInfo<Serve> page= serveService.selectWeiFanKui();
        mav.addObject("page",page);
        return mav;
    }

    @GetMapping("/FuWuGuanLi/serveFanKuiNew")
    public ModelAndView ServeFanKuiNewView(
            @RequestParam(name = "id",required = true)int id
    ){
        ModelAndView mav=new ModelAndView("FuWuGuanLi/serveFanKuiNew");
        Serve serve=serveService.findById(id);
        mav.addObject("serve",serve);
        return mav;
    }

    @PostMapping("/FuWuGuanLi/serveFanKuiNew")
    //Validated注解所标注的参数，表面该参数中的属性取值必须要按照属性上定义的验证规则进行验证，验证是由验证框架完成的
    //验证的结果会以BindingResult实例的形式传入到该方法
    public ModelAndView ServeFanKuiNew(
            Serve serve
    ){
        String redirectUrl="redirect:/FuWuGuanLi/serveFanKui";
        ModelAndView mav=new ModelAndView(redirectUrl);
        Date time = new java.util.Date(new java.util.Date().getTime());
        serve.setUpdateDate(time);
        boolean result=serveService.modify(serve);
        if(result){
            logger.info("反馈成功 - " + serve);
        }else {
            logger.info("反馈失败 - " + serve);
        }
        return mav;
    }

    @GetMapping("/FuWuGuanLi/serveFenPei")
    public ModelAndView ServeFenPeiView(){
        ModelAndView mav=new ModelAndView("FuWuGuanLi/serveFenPei");
        PageInfo<Serve> page= serveService.selectWeiFenPei();
        mav.addObject("page",page);
        return mav;
    }

    @GetMapping("/FuWuGuanLi/serveFenPeiNew")
    public ModelAndView ServeFenPeiNewView(
            @RequestParam(name="id" ,required = true)int id
    ){
        ModelAndView mav=new ModelAndView("FuWuGuanLi/serveFenPeiNew");
        Serve serve=serveService.findById(id);
        mav.addObject("serve",serve);
        return mav;
    }

    @PostMapping("/FuWuGuanLi/serveFenPeiNew")
    //Validated注解所标注的参数，表面该参数中的属性取值必须要按照属性上定义的验证规则进行验证，验证是由验证框架完成的
    //验证的结果会以BindingResult实例的形式传入到该方法
    public ModelAndView ServeFenPeiNew(
            Serve serve
    ){
        String redirectUrl="redirect:/FuWuGuanLi/serveFenPei";
        ModelAndView mav=new ModelAndView(redirectUrl);
        Date time = new java.util.Date(new java.util.Date().getTime());
        serve.setUpdateDate(time);
        serve.setAssignTime(time);
        boolean result=serveService.modify(serve);
        if(result){
            logger.info("分配服务成功 - " + serve);
        }else {
            logger.info("分配服务失败 - " + serve);
        }
        return mav;
    }


    @GetMapping("/FuWuGuanLi/serveGuiDang")
    public ModelAndView ServeGuiDangView(){
        ModelAndView mav=new ModelAndView("FuWuGuanLi/serveGuiDang");
        PageInfo<Serve> page= serveService.selectYiWanCheng();
        mav.addObject("page",page);
        return mav;
    }





}
